#!/usr/bin/perl

use strict;

use Sleeper qw(resetTimer sleepN SleeperN);
use PerlUtil;

sub printNow
{
    my $now_string = localtime;
    print "$now_string\n";
}

#1. using object
print "Using object\n";
my $sleeper = Sleeper->new(2);

$sleeper->reset; printNow;

foreach (1 .. 5) {
   $_ > 2 ? $sleeper->sleep(3) : $sleeper->sleep;
   printNow;
}

#2. using function object
print "Using function object\n";
my $sleep2 = SleeperN(2);
my $sleep3;
printNow;

foreach (1 .. 5) {
   $sleep3 = SleeperN(3) if ($_ == 3);

   $_ > 2 ? &$sleep3 : &$sleep2;
   printNow;
}

#3. using closure
print "Using closure\n";
resetTimer; printNow;
 
foreach (1 .. 5) {
   $_ >  2 ? sleepN(3) : sleepN(2);
   printNow;
}

#4. using thread
print "Using thread\n";
for (1 .. 5) {
    $_ > 2 ? runForSeconds(3, \&printNow)
           : runForSeconds(2, \&printNow);
}
printNow;
